package com.ficus.road.maintain.core.model.zh.xlj;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/6/27 上午10:35
 */
@Data
@Slf4j
public class XljWarningFlowZH {
    private String warningId;
    // 下立交id
    private String lzid;
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private Date createtime;

    private String xljName;

    private String INFO;

    private String REMARK;

    // 是否发送交警 0未发送, 1已发送
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private Date sendTraPoli;
    // 是否发送水务 0未发送, 1已发送
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private Date sendWatServ;
    // 是否发送管理部门 0未发送, 1已发送
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd'T'HH:mm:ss")

    private Date sendManCent;
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private Date MessageTime;

    private List<XljStatusZH> XLJ_STATUS;

    private List<XljWarningFlow.XljWarningInfo> INFO_LIST;

    public XljWarningFlow convert2XljWarningFlow(XljWarningFlowZH zh, Date date) {
        return XljWarningFlow.builder()
                .warningId(zh.getWarningId())
                .xljId(zh.getLzid())
                .createTime(zh.getCreatetime())
                .xljName(zh.getXljName())
                .sendTraPolice(zh.getSendTraPoli())
                .sendWatServ(zh.getSendWatServ())
                .sendManCent(zh.getSendManCent())
                .messageTime(zh.getMessageTime())
                .xljStatusList(zh.getXLJ_STATUS().stream().map(item -> item.convert2XljStatus(item)).collect(Collectors.toList()))
                .xljWarningInfoList(zh.getINFO_LIST())
                .syncTime(date)
                .build();
    }

    @Data
    private static class XljStatusZH {
        //1:到达 2:确认 3:封交 4:解除封交
        private Integer xljCondition;
        // 状态更新时间
        @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
        private Date condTime;
        // 状态更新图片
        private String condImgUrl;
        // 目前水位（单位:cm）-1：无水位信息
        private Integer waterLevel;

        public XljWarningFlow.XljStatus convert2XljStatus(XljStatusZH zh) {
            String imgUrl = null;
            try {
                if (StringUtils.isNotEmpty(zh.getCondImgUrl())) {
                    String[] split = zh.getCondImgUrl().split(";");
                    imgUrl = split[0];
                }
            } catch (Exception e) {
                log.error("转化下立交图片异常: {}", e);
            }
            return XljWarningFlow.XljStatus.builder()
                    .xljCondition(zh.getXljCondition())
                    .condTime(zh.getCondTime())
                    .condImgUrl(imgUrl)
                    .waterLevel(zh.getWaterLevel())
                    .build();
        }
    }
}
